//
//  ViewController.m
//  DemoTouchID
//
//  Created by 唐可雄 on 16/6/2.
//  Copyright © 2016年 swustyc. All rights reserved.
//

#import "ViewController.h"

@import LocalAuthentication;

@interface ViewController ()<UITableViewDataSource, UITableViewDelegate> {
    NSMutableArray *_tempArray;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    _tempArray = [[NSMutableArray alloc] init];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UITableViewDataSource, UITableViewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _tempArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44.f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
    }
    
    cell.textLabel.text = _tempArray[indexPath.row];
    
    return cell;
}

#pragma mark - IBAction

/// 点击指纹验证
- (IBAction)fingerPrintBtnDidClick:(id)sender {
    // 检查touch id是否可用
    LAContext *context = [[LAContext alloc] init];
    if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:NULL]) {
        [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"指纹验证" reply:^(BOOL success, NSError * _Nullable error) {
            if (success) {
                [_tempArray addObject:@"验证通过"];
            } else {
                switch (error.code) {
                    case LAErrorAppCancel:
                        [_tempArray addObject:@"应用程序取消"];
                        break;
                    case LAErrorInvalidContext:
                        [_tempArray addObject:@"上下文失效"];
                        break;
                    case LAErrorTouchIDLockout:
                        [_tempArray addObject:@"TouchID被锁定"];
                        break;
                    case LAErrorTouchIDNotEnrolled:
                        [_tempArray addObject:@"指纹未录入"];
                        break;
                    case LAErrorTouchIDNotAvailable:
                        [_tempArray addObject:@"TouchID不可用"];
                        break;
                    case LAErrorPasscodeNotSet:
                        [_tempArray addObject:@"密码为设置"];
                        break;
                    case LAErrorSystemCancel:
                        [_tempArray addObject:@"系统取消"];
                        break;
                    case LAErrorUserFallback:
                        [_tempArray addObject:@"用户选择输入密码"];
                        break;
                    case LAErrorUserCancel:
                        [_tempArray addObject:@"用户取消"];
                        break;
                    case LAErrorAuthenticationFailed:
                        [_tempArray addObject:@"认证失败"];
                        break;
                    default:
                        break;
                }
            }
            
            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                [self.tableView reloadData];
            }];
        }];
    } else {
        
    }
}

@end
